﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IFileExplorerForm_EditNodeFilters</summary>
    public class IFileExplorerForm_EditNodeFilters : IForm
    {
        public INodeFilters Value; // Objekt pre editaciu

        protected IGroupBox FGrp; // Controls
        protected IButton FBtn_OK;
        protected IButton FBtn_Cancel;
        protected IListView FList;
        protected IRadioButton FRadio_OR, FRadio_AND;

        /// <summary>Konstruktor</summary>
        public IFileExplorerForm_EditNodeFilters()
        {
            Value = new INodeFilters();
        }
        /// <summary>Dispose metoda</summary>
        /// <param name="disposing">parameter</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing); // Volam predchodcu
            Value = (INodeFilters)ICore.DisposeObject(Value);
        }

        /// <summary>Metoda: OnCreateForm</summary>
        /// <returns>true / false</returns>
        protected override bool OnCreateForm()
        {
            if (!base.OnCreateForm()) return false; // Volam predchodcu

            // Inicializacia
            MinimizeBox = false; MaximizeBox = false; FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; ShowIcon = true; ShowInTaskbar = false; StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            SetBounds(0, 0, 678, 510); System.Drawing.Size CS = ClientSize;

            // Vytvorim groupbox
            FGrp = new IGroupBox(); IControl.PlaceControl_Inside(FGrp, this, 10, 10, 10, 50);

            // Vytvorim tlacidla
            FBtn_Cancel = new IButton(lang.FORM_EDIT_FILTERS_TEXT_3); IControl.PlaceControl_Outside_TopRight_RightBottom(FBtn_Cancel, FGrp, 0, 10); FBtn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            FBtn_OK = new IButton(lang.FORM_EDIT_FILTERS_TEXT_2, null); IControl.PlaceControl_Outside_TopRight_LeftTop(FBtn_OK, FBtn_Cancel, 0, 10); FBtn_OK.DialogResult = System.Windows.Forms.DialogResult.OK;

            FList = new IListView(); IControl.PlaceControl_Inside(FList, FGrp, 4, 8, 4, 40); // Zoznam podmienok
            FList.MultiSelect = false; FList.FullRowSelect = true; FList.CreateColumn(lang.FORM_EDIT_FILTERS_TEXT_6, FList.ClientSize.Width - 16, System.Windows.Forms.HorizontalAlignment.Left);
            FList.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(); FList.ContextMenuStrip.Opening += Event_ContentMenuOpening; IToolStrip.CreateSeparator(FList.ContextMenuStrip.Items);
            IToolTip T = new IToolTip(); T.SetToolTip(FList, lang.FORM_EDIT_FILTERS_TEXT_13);

            FRadio_OR = new IRadioButton(lang.FORM_EDIT_FILTERS_TEXT_4); // Typ logickeho spojenia
            FRadio_AND = new IRadioButton(lang.FORM_EDIT_FILTERS_TEXT_5); 
            FGrp.Controls.AddRange(new System.Windows.Forms.Control[] { FList, FRadio_OR, FRadio_AND }); // Zaradim do controls
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FRadio_OR, FList, 20, 10);
            IControl.PlaceControl_Outside_LeftTop_TopRight(FRadio_AND, FRadio_OR, 0, 30);
            
            Controls.AddRange(new System.Windows.Forms.Control[] { FGrp, FBtn_OK, FBtn_Cancel }); // Zaradim do controls

            FList.ItemActivate += Event_MenuItemClick_Edit; // Nastavim eventy
            FRadio_AND.CheckedChanged += Event_CheckedChanged; FRadio_OR.CheckedChanged += Event_CheckedChanged;

            AcceptButton = FBtn_OK; CancelButton = FBtn_Cancel; FBtn_OK.Focus();

            FillForm(); // Vyplnim zaciatocnou hodnotou
            return true; // Vsetko OK
        }

        /// <summary>Vyplnenie formulara</summary>
        protected void FillForm()
        {
            FList.BeginCreate();
            FList.Items.Clear(); // Vymazem zoznam

            for (int i = 0; i < Value.Count; i++)
            {
                FList.AddItem(null, Value[i].DisplayValue, i);
            }

            FList.EndCreate();

            FRadio_OR.Checked = Value.LogOperator == INodeFilters.LOG_OPERATOR_OR;
            FRadio_AND.Checked = Value.LogOperator == INodeFilters.LOG_OPERATOR_AND;
        }

        /// <summary>Event: OnClick pre polozku menu 'Clear'</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MenuItemClick_Clear(object pSender, EventArgs e)
        {
            if (!IApp.ShowQuestion(lang.FORM_EDIT_FILTERS_TEXT_7)) return; // Opytam sa
            Value.Clear(); FillForm(); // Vymazem obsah a aktualizujem okno
        }
        /// <summary>Event: Opening pre FList.ContextMenuStrip</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_ContentMenuOpening(object pSender, System.ComponentModel.CancelEventArgs e)
        {
            System.Windows.Forms.ContextMenuStrip M = (System.Windows.Forms.ContextMenuStrip)pSender;
            M.Items.Clear();

            IToolStripMenuItem M1, M2, M3, M4;

            IToolStrip.CreateMenuItem(M.Items, lang.FORM_EDIT_FILTERS_TEXT_8_1, Event_MenuItemClick_AddNew);
            IToolStrip.CreateSeparator(M.Items);
            IToolStrip.CreateMenuItem(M.Items, lang.FORM_EDIT_FILTERS_TEXT_8_4, Event_MenuItemClick_Edit);
            IToolStrip.CreateSeparator(M.Items);
            IToolStrip.CreateMenuItem(M.Items, lang.FORM_EDIT_FILTERS_TEXT_8_2, Event_MenuItemClick_Delete);
            IToolStrip.CreateMenuItem(M.Items, lang.FORM_EDIT_FILTERS_TEXT_8_3, Event_MenuItemClick_Clear);

            IToolStrip.CreateSeparator(M.Items);
            M1 = IToolStrip.CreateMenuItem(M.Items, lang.FORM_EDIT_FILTERS_TEXT_11_1);
            M2 = IToolStrip.CreateMenuItem(M1.DropDownItems, lang.FORM_EDIT_FILTERS_TEXT_11_2);
            M3 = IToolStrip.CreateMenuItem(M2.DropDownItems, lang.FORM_EDIT_FILTERS_TEXT_11_5);

            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_DIRS, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_DIR;
            IToolStrip.CreateSeparator(M3.DropDownItems);
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_TEXT, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_TEXT;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_DATA, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_DATA;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_AUDIO, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_AUDIO;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_VIDEO, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_VIDEO;
            IToolStrip.CreateSeparator(M3.DropDownItems);
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_3D_IMAGE, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_3D_IMAGE;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_RASTER_IMAGE, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_RASTER_IMAGE;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_VECTOR_IMAGE, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_VECTOR_IMAGE;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_PAGE_LAYOUT, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_PAGE_LAYOUT;
            IToolStrip.CreateSeparator(M3.DropDownItems);
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_SPREAD_SHEET, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_SPREAD_SHEET;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_DATABASE, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_DATABASE;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_GAME, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_GAME;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_CAD, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_CAD;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_GIS, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_GIS;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_WEB, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_WEB;
            IToolStrip.CreateSeparator(M3.DropDownItems);
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_EXE, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_EXE;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_PLUGIN, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_PLUGIN;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_FONT, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_FONT;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_SYSTEM, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_SYSTEM;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_SETTINGS, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_SETTINGS;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_BACKUP, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_BACKUP;
            IToolStrip.CreateSeparator(M3.DropDownItems);
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_ENCODED, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_ENCODED;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_ARCHIVES, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_COMPRESSED;
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_DISK_IMAGE, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_DISK_IMAGE;
            IToolStrip.CreateSeparator(M3.DropDownItems);
            M4 = IToolStrip.CreateMenuItem(M3.DropDownItems, lang.MENU_ITEM_TEXT_FILE_EXPLORER_FILTER_ITEMS_DEVEL, Event_MenuItemClick_LoadPredefined); M4.Tag = INodeFilters.PREDDEFINED_ID_DEVEL;

            if (IApp.GlobalData.FileExplorer_Filters.Count > 0) // Zoznam ulozenych filtrov pre nacitanie
            {
                IToolStrip.CreateSeparator(M2.DropDownItems);
                for (int i = 0; i < IApp.GlobalData.FileExplorer_Filters.Count; i++)
                {
                    IToolStrip.CreateMenuItem(M2.DropDownItems, IApp.GlobalData.FileExplorer_Filters.GetName(i), Event_MenuItemClick_Load).Tag = i;
                }
            }

            IToolStrip.CreateSeparator(M1.DropDownItems);
            M2 = IToolStrip.CreateMenuItem(M1.DropDownItems, lang.FORM_EDIT_FILTERS_TEXT_11_3);
            IToolStrip.CreateMenuItem(M2.DropDownItems, lang.FORM_EDIT_FILTERS_TEXT_11_7, Event_MenuItemClick_SaveNew);

            if (IApp.GlobalData.FileExplorer_Filters.Count > 0) // Zoznam ulozenych filtrov pre nahradenie
            {
                IToolStrip.CreateSeparator(M2.DropDownItems);
                M3 = IToolStrip.CreateMenuItem(M2.DropDownItems, lang.FORM_EDIT_FILTERS_TEXT_11_8);
                for (int i = 0; i < IApp.GlobalData.FileExplorer_Filters.Count; i++)
                {
                    IToolStrip.CreateMenuItem(M3.DropDownItems, IApp.GlobalData.FileExplorer_Filters.GetName(i), Event_MenuItemClick_Replace).Tag = i;
                }
            }

            IToolStrip.CreateSeparator(M1.DropDownItems);
            M2 = IToolStrip.CreateMenuItem(M1.DropDownItems, lang.FORM_EDIT_FILTERS_TEXT_11_4);
            IToolStrip.CreateMenuItem(M2.DropDownItems, lang.FORM_EDIT_FILTERS_TEXT_11_6, Event_MenuItemClick_ClearStorage);

            if (IApp.GlobalData.FileExplorer_Filters.Count > 0) // Zoznam ulozenych filtrov pre nahradenie
            {
                IToolStrip.CreateSeparator(M2.DropDownItems);
                for (int i = 0; i < IApp.GlobalData.FileExplorer_Filters.Count; i++)
                {
                    IToolStrip.CreateMenuItem(M2.DropDownItems, IApp.GlobalData.FileExplorer_Filters.GetName(i), Event_MenuItemClick_DeleteFromStorage).Tag = i;
                }
            }
            
        }
        /// <summary>Event: OnClick pre polozku menu 'Pridat'</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MenuItemClick_AddNew(object pSender, EventArgs e)
        {
            INodeFilter F = new INodeFilter(); // Vytvorim objekt
            if (!F.EditWithForm(lang.FORM_EDIT_FILTERS_TEXT_9)) // Editujem
            {
                F.Dispose(); // Zrusena editacia
                return;
            }
            Value.AddFilter(F); // Zaradim
            FillForm(); // Aktualizujem zoznam
            FList.SelectedIndex = Value.Count - 1;
        }
        /// <summary>Event: OnClick pre polozku menu 'Edit'</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MenuItemClick_Edit(object pSender, EventArgs e)
        {
            int I = FList.SelectedIndex;
            if (I == -1) return; // Ziaden aktivny filter
            if (!Value[I].EditWithForm(lang.FORM_EDIT_FILTERS_TEXT_10)) return; // Editujem
            FillForm(); FList.SelectedIndex = I;
        }
        /// <summary>Event: OnClick pre polozku menu 'Delete'</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MenuItemClick_Delete(object pSender, EventArgs e)
        {
            int I = FList.SelectedIndex;
            if (I == -1) return; // Ziaden aktivny filter
            
            if (!IApp.ShowQuestion(lang.FORM_EDIT_FILTERS_TEXT_7_1)) return; // Opytam sa na vymazanie

            Value.DeleteFilter(I); // Vymazem
            FillForm(); // Aktualizujem list
            
            if (FList.ItemsCount != 0) // Oznacim polozku
            {
                if (I >= FList.ItemsCount) I = FList.ItemsCount - 1;
                FList.SelectedIndex = I;
            }
        }
        /// <summary>Event: CheckedChanged pre radio tlacidla</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_CheckedChanged(object pSender, EventArgs e)
        {
            Value.LogOperator = FRadio_OR.Checked ? INodeFilters.LOG_OPERATOR_OR : INodeFilters.LOG_OPERATOR_AND;
        }
        /// <summary>Event: Click na polozku menu - nacita preddefinovane</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MenuItemClick_LoadPredefined(object pSender, EventArgs e)
        {
            if (!IApp.ShowQuestion(lang.FORM_EDIT_FILTERS_TEXT_12_5)) return; // Potvrdim load
            Value.LoadPreddefined(ICore.ToInt(((IToolStripMenuItem)pSender).Tag));
            FillForm(); 
        }
        /// <summary>Event: Click na polozku menu - ulozenie noveho filtra</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MenuItemClick_SaveNew(object pSender, EventArgs e)
        {
            string Name = "";
            if (!IApp.ShowEditor_String(lang.FORM_EDIT_FILTERS_TEXT_12_1, lang.FORM_EDIT_FILTERS_TEXT_12_2, ref Name, 32, false, "FILE_EXPLORER_FILTER_NAME")) return; // Ziskam nazov od uzivatela
            IApp.GlobalData.FileExplorer_Filters.Add(Name, Value.Serialize()); // Ulozim do databazy
        }
        /// <summary>Event: Click na polozku menu - nacitat filter</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MenuItemClick_Load(object pSender, EventArgs e)
        {
            if (!IApp.ShowQuestion(lang.FORM_EDIT_FILTERS_TEXT_12_3)) return; // Potvrdim load
            Value.Deserialize(IApp.GlobalData.FileExplorer_Filters.GetValue(ICore.ToInt(((IToolStripMenuItem)pSender).Tag)));
            FillForm();
        }
        /// <summary>Event: Click na polozku menu - nahradenie existujuceho filtra</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MenuItemClick_Replace(object pSender, EventArgs e)
        {
            if (!IApp.ShowQuestion(lang.FORM_EDIT_FILTERS_TEXT_12_4)) return; // Potvrdim prepisanie
            IApp.GlobalData.FileExplorer_Filters.ReplaceValue(ICore.ToInt(((IToolStripMenuItem)pSender).Tag), Value.Serialize());
        }
        /// <summary>Event: Click na polozku menu - vymazanie vsetkych filtrov</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MenuItemClick_ClearStorage(object pSender, EventArgs e)
        {
            if (!IApp.ShowQuestion(lang.FORM_EDIT_FILTERS_TEXT_12_6)) return; // Potvrdim vycistenie
            IApp.GlobalData.FileExplorer_Filters.Clear(); // Vymazem vsetky
        }
        /// <summary>Event: Click na polozku menu - vymazanie filtrov zo storage</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MenuItemClick_DeleteFromStorage(object pSender, EventArgs e)
        {
            int Index = ICore.ToInt(((IToolStripMenuItem)pSender).Tag);
            if (!IApp.ShowQuestion(string.Format(lang.FORM_EDIT_FILTERS_TEXT_12_7, IApp.GlobalData.FileExplorer_Filters.GetName(Index)))) return; // Potvrdim vymazanie
            IApp.GlobalData.FileExplorer_Filters.Delete(Index); // Vymazem zaznam
        }
    }
}
